package com.zsj.comminlibrary.base;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;

import com.google.android.material.snackbar.Snackbar;
import com.zsj.comminlibrary.R;
import com.zsj.comminlibrary.util.BgUtils;
import com.zsj.comminlibrary.util.SpManager;
import com.zsj.comminlibrary.view.dialog.LoadingDialog;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import io.alterac.blurkit.BlurLayout;

/**
 * Created by jie on 2017/6/23.
 */

public abstract class BaseActivity<P extends BasePresenter> extends AppCompatActivity implements IView {
    public P mPresenter;
    private Context mContext;
    private LoadingDialog mLoadingDialog;
    private View mRootView;
    protected List<BlurLayout> mBlurLayouts;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        //全屏
        getWindow().setFlags(WindowManager.LayoutParams. FLAG_FULLSCREEN ,
                WindowManager.LayoutParams. FLAG_FULLSCREEN);
        mContext = this;
        BaseApplication.addActivity(this);
        mBlurLayouts = new ArrayList<>();
        initView();
        setViewBackground();
        mPresenter = TUtil.getT(this, 0);
        mLoadingDialog = new LoadingDialog.Builder(this).build();
        if (mPresenter != null) {
            mPresenter.onSetView(this);
            mPresenter.mContext = mContext;
            mPresenter.onCreate();
        }
        initData();
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }

    }

    protected void setViewBackground() {
        View view = getRootView();
        if (view == null) {
            view = ((ViewGroup) findViewById(android.R.id.content)).getChildAt(0);
        }
        if (view != null) {
            int bgId = SpManager.getInstance().getBgId();
            if (bgId != -1) {
                view.setBackgroundResource(bgId);
            } else {
                String bgName = SpManager.getInstance().getBgName();
                Bitmap bitmap = BgUtils.getInstance().getBitmap(bgName);
                if (bitmap != null) {
                    view.setBackgroundDrawable(new BitmapDrawable(bitmap));
                } else {
                    view.setBackgroundResource(R.mipmap.bg);
                }
            }
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
        if (mPresenter != null) {
            mPresenter.onStart();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        BaseViewNavigator.setContext(this);
        if (mPresenter != null) {
            mPresenter.onResume();
        }

    }

    @Override
    protected void onPause() {
        super.onPause();
        if (mPresenter != null) {
            mPresenter.onPause();
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        if (mPresenter != null) {
            mPresenter.onStop();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mPresenter != null) {
            mPresenter.onDestroy();
        }
        BaseApplication.removeActivity(this);
    }

    public void showLoading(String msg) {
        if (mLoadingDialog.isShowing())
            return;
        mLoadingDialog.setTitle(msg);
        mLoadingDialog.cancel();
        mLoadingDialog.setCanceledOnTouchOutside(true);
        mLoadingDialog.show();
    }

    public void hideLoading() {
        if (mLoadingDialog != null) {
            mLoadingDialog.dismiss();
        }
    }

    @Override
    public void showSnackbar(String msg) {
        View view = getRootView();
        if (view == null) {
            view = ((ViewGroup) findViewById(android.R.id.content)).getChildAt(0);
        }
        Snackbar.make(view, msg, 3000)
                .show();
    }

    public View getRootView() {
        return null;
    }

    @Override
    public void showToast(String msg) {
        Toast.makeText(mContext, msg, Toast.LENGTH_SHORT).show();
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                onBackPressed();
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void exit() {
        this.finish();
    }

    protected abstract void initView();

    protected abstract void initData();

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onChangeBgEvent(ChangBgEvent event) {
        setViewBackground();
    }

}
